//
//  AddViewController.m
//  Homework_17(2)
//
//  Created by 徐昊 on 16/8/6.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "AddViewController.h"


@interface AddViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (nonatomic, strong) Contact *contact;
@property (weak, nonatomic) IBOutlet UIImageView *image;
@property (weak, nonatomic) IBOutlet UITextField *name;
@property (weak, nonatomic) IBOutlet UITextField *phoneNumber;
@property (weak, nonatomic) IBOutlet UITextField *address;

@end

@implementation AddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createTap];
}

- (void)createTap
{
    self.image.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handlePhoto:)];
    [self.image addGestureRecognizer:tap];
}

- (void)handlePhoto:(UITapGestureRecognizer *)tap
{
    
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    //    UIImagePickerControllerSourceType *sourceType = UIImagePickerControllerCameraCaptureModePhoto;
    //    picker.sourceType = sourceType;
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
    [self presentViewController:picker animated:YES completion:^{
        
    }];
    
}


//- (void)imagePickerController:(UIImagePickerController *)picker
//        didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
//{
//    self.image.image = image;
//    
//    [picker dismissViewControllerAnimated:YES completion:^{
//        
//    }];
//}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    _image.image = image;
    [picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)clickButton:(id)sender {
    _contact = [[Contact alloc] init];
    _contact.name = _name.text;
    _contact.phoneNumber = _phoneNumber.text;
    _contact.address = _address.text;
    _contact.image = @"3";
    if (self.delegate && [self.delegate respondsToSelector:@selector(addContact:)]) {
        NSLog(@"%@", _contact.name);
        [self.delegate addContact:_contact];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
